package com.yw.smartcityforjava.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;
import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.adapter.MyBannerAdapter;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.utils.SPUtils;

import java.util.ArrayList;

public class SplashActivity extends AppCompatActivity implements View.OnClickListener {

    private Banner banner;
    private Button btnNetSet;
    private Button btnLogin;
    private ArrayList images = new ArrayList();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //判断是否要显示引导页
        if(!SPUtils.getInstance().getBoolean(SPUtils.Key.SHOW_GUIDE, true)){
            gotoLogin();
            return;
        }

        setContentView(R.layout.activity_splash);
        initView();
        initData();

        //设置图片加载器
        banner.setAdapter(new MyBannerAdapter(images));
        banner.setIndicator(new CircleIndicator(App.app));
        banner.start();

        btnLogin.setOnClickListener(this);
        btnNetSet.setOnClickListener(this);

        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if(i == images.size()-1){
                    btnLogin.setVisibility(View.VISIBLE);
                    btnNetSet.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    protected void initView() {
        banner = (Banner) findViewById(R.id.banner);
        btnNetSet = (Button) findViewById(R.id.btn_netSet);
        btnLogin = (Button) findViewById(R.id.btn_login);
    }

    protected void initData() {
        images.add(R.drawable.smartcity1);
        images.add(R.drawable.smartcity2);
        images.add(R.drawable.smartcity3);
        images.add(R.drawable.smartcity4);
        images.add(R.drawable.smartcity5);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                //设置标记位
                SPUtils.getInstance().saveBoolean(SPUtils.Key.SHOW_GUIDE, false);
                gotoLogin();
                break;
            case R.id.btn_netSet:
                EditText et = new EditText(this);
                et.setHint("输入格式：192.168.0.1:10001");
                ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.leftMargin = 20;
                layoutParams.rightMargin = 20;
                et.setLayoutParams(layoutParams);
                new AlertDialog.Builder(this)
                        .setTitle("端口IP设置")
                        .setView(et)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String ip = et.getText().toString().trim();
                                if(TextUtils.isEmpty(ip)){
                                    Toast.makeText(App.app, "请输入ip地址", Toast.LENGTH_LONG ).show();
                                }else{
                                    //保存ip地址
                                    SPUtils.getInstance().saveString("sp_ip", ip);
                                    App.baseUrl = ip;
                                    dialog.cancel();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).setCancelable(false).show();
                break;
        }
    }

    private void gotoLogin() {
        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        finish();
    }
}
